﻿using System;
using UnityEngine;
using UnityEngine.UIElements;
using winS.UnityEditor.UIElement;
using static winS.UnityEditor.ProcessEditor.ProcessEditorData;
using Button = winS.UnityEditor.UIElement.Button;
using ColorField = winS.UnityEditor.UIElement.ColorField;
using Column = winS.UnityEditor.UIElement.Column;
using FloatField = winS.UnityEditor.UIElement.FloatField;
using Label = winS.UnityEditor.UIElement.Label;

namespace winS.UnityEditor.ProcessEditor
{
    /// <summary>
    /// 流程编辑器设置窗口
    /// </summary>
    public class SettingWindow : VisualElement
    {
        private readonly Color titleColor = new Color32(27, 27, 27, 255);
        private readonly Color buttonColor = new Color32(40, 40, 40, 255);
        private readonly Color borderColor = new Color32(30, 30, 30, 255);
        private readonly Color backgroundColor = new Color32(56, 56, 56, 255);

        private readonly HistoryData historyData;
        private readonly BackgroundData backgroundData;
        private readonly SelectionSetting selectionSetting;
        private readonly ConnectionLineSetting connectionSetting;

        internal SettingWindow(ProcessEditorData processEditorData)
        {
            historyData = processEditorData.historyData;
            backgroundData = processEditorData.backgroundData;
            selectionSetting = processEditorData.selectionSetting;
            connectionSetting = processEditorData.connectionLineSetting;

            style.alignSelf = Align.Center;
            style.overflow = Overflow.Hidden;
            style.position = Position.Absolute;

            style.SetBorderWidth(1f);
            style.SetBorderRadius(8f);
            style.SetBorderColor(borderColor);
            style.SetTop(25f, LengthUnit.Percent);
            style.SetWidth(24f, LengthUnit.Percent);
            style.SetHeight(50f, LengthUnit.Percent);
            style.SetBackgroundColor(backgroundColor);

            Add(CreateTitle());

            SidebarView sidebarView = EditorElementUtility.elementFactory.CreateSidebarView();
            sidebarView.CreatePage("历史记录", CreateHistorySetting()).normalColor = buttonColor;
            sidebarView.CreatePage("样式设置", CreateStyleSetting()).normalColor = buttonColor;
            sidebarView.sidebar.splitlineColor = new Color32(244, 244, 244, 255);
            Add(sidebarView);
        }

        private VisualElement CreateTitle()
        {
            Row row = EditorElementUtility.elementFactory.CreateRow();
            row.style.SetBackgroundColor(titleColor);
            row.style.maxHeight = 30f;

            CreateColumn();

            Column columnMiddle = CreateColumn();
            columnMiddle.style.justifyContent = Justify.Center;
            Label label = EditorElementUtility.elementFactory.CreateLabel("设置");
            label.style.fontSize = 14f;
            columnMiddle.Add(label);

            Column columnRight = CreateColumn();
            columnRight.style.alignItems = Align.FlexEnd;
            Button closeButton = EditorElementUtility.elementFactory.CreateButton(Resources.Load<Texture>("winS.UnityEditor.ProcessEditor/Icon/关闭"), () => style.display = DisplayStyle.None);
            closeButton.style.SetBorderColor(Color.clear);
            closeButton.normalColor = Color.clear;
            closeButton.icon.tintColor = Color.red;
            closeButton.style.borderTopRightRadius = 8f;
            columnRight.Add(closeButton);

            Column CreateColumn()
            {
                Column column = EditorElementUtility.elementFactory.CreateColumn();
                column.style.SetWidth(33.333f, LengthUnit.Percent);
                row.Add(column);
                return column;
            }

            return row;
        }
        private VisualElement CreateHistorySetting()
        {
            ScrollView content = EditorElementUtility.elementFactory.CreateScrollView(ScrollViewMode.Vertical);

            content.Add(EditorElementUtility.elementFactory.CreateIntField("最大历史记录:", historyData.maxHistory, newValue => historyData.maxHistory = Mathf.Max(1, newValue)));
            content.Add(EditorElementUtility.elementFactory.CreateButton("清除历史记录数据", () => historyData.Clear(), 32f));

            return content;
        }
        private VisualElement CreateStyleSetting()
        {
            ScrollView content = EditorElementUtility.elementFactory.CreateScrollView(ScrollViewMode.Vertical);
            content.Add(CreateBackgroundStyleSetting());
            content.Add(CreateConnectionLineStyleSetting());
            content.Add(CreateSelectionSetting());
            return content;
        }

        private VisualElement CreateBackgroundStyleSetting()
        {
            Column column = EditorElementUtility.elementFactory.CreateColumn();
            column.style.marginTop = 2f;

            Label backgroundLabel = EditorElementUtility.elementFactory.CreateLabel("背景样式", 14f);
            backgroundLabel.style.alignSelf = Align.FlexStart;
            backgroundLabel.style.unityFontStyleAndWeight = FontStyle.Bold;
            backgroundLabel.style.marginLeft = EditorElementUtility.defaultMarginHorizontal;
            column.Add(backgroundLabel);

            Row row = EditorElementUtility.elementFactory.CreateRow();
            Label label = EditorElementUtility.elementFactory.CreateLabel("线条间隔:");
            label.style.marginLeft = 3f;
            row.Add(label);
            FloatField minIntervalField = EditorElementUtility.elementFactory.CreateFloatField("最小", backgroundData.minInterval);
            minIntervalField.style.flexGrow = 1f;
            minIntervalField.onValueChanged += newValue =>
            {
                if (newValue < 1)
                {
                    minIntervalField.SetValueWithoutNotify(1);
                    return;
                }
                if (newValue > backgroundData.maxInterval)
                {
                    minIntervalField.SetValueWithoutNotify(backgroundData.maxInterval);
                    return;
                }
                backgroundData.minInterval = newValue;
            };
            row.Add(minIntervalField);

            FloatField maxIntervalField = EditorElementUtility.elementFactory.CreateFloatField("最大", backgroundData.maxInterval);
            maxIntervalField.style.flexGrow = 1f;
            maxIntervalField.onValueChanged += newValue =>
            {
                if (newValue > 100)
                {
                    maxIntervalField.SetValueWithoutNotify(100);
                    return;
                }
                if (newValue < backgroundData.minInterval)
                {
                    maxIntervalField.SetValueWithoutNotify(backgroundData.minInterval);
                    return;
                }
                backgroundData.maxInterval = newValue;
            };
            row.Add(maxIntervalField);

            column.Add(AddToContent(row, () =>
            {
                minIntervalField.value = BackgroundData.minIntervalDefaultValue;
                maxIntervalField.value = BackgroundData.maxIntervalDefaultValue;
            }));

            FloatField lineWidthField = EditorElementUtility.elementFactory.CreateFloatField("线条宽度:", backgroundData.lineWidth, newValue => backgroundData.lineWidth = newValue);
            column.Add(AddToContent(lineWidthField, () => lineWidthField.value = BackgroundData.lineWidthDefaultValue));

            IntField rulerlineIntervalField = EditorElementUtility.elementFactory.CreateIntField("标尺间隔:", backgroundData.rulerlineInterval, newValue => backgroundData.rulerlineInterval = newValue);
            column.Add(AddToContent(rulerlineIntervalField, () => rulerlineIntervalField.value = BackgroundData.rulerlineIntervalDefaultValue));

            ColorField lineColorField = EditorElementUtility.elementFactory.CreateColorField("线条颜色:", backgroundData.lineColor, newValue => backgroundData.lineColor = newValue);
            column.Add(AddToContent(lineColorField, () => lineColorField.value = BackgroundData.lineColorDefaultValue));

            ColorField rulerlineColorField = EditorElementUtility.elementFactory.CreateColorField("标尺颜色:", backgroundData.rulerlineColor, newValue => backgroundData.rulerlineColor = newValue);
            column.Add(AddToContent(rulerlineColorField, () => rulerlineColorField.value = BackgroundData.rulerlineColorDefaultValue));

            ColorField backgroundColorField = EditorElementUtility.elementFactory.CreateColorField("背景颜色:", backgroundData.backgroundColor, newValue => backgroundData.backgroundColor = newValue);
            column.Add(AddToContent(backgroundColorField, () => backgroundColorField.value = BackgroundData.backgroundColorDefaultValue));

            return column;
        }

        private VisualElement CreateConnectionLineStyleSetting()
        {
            Column column = EditorElementUtility.elementFactory.CreateColumn();
            column.style.marginTop = 2f;

            Label headLabel = EditorElementUtility.elementFactory.CreateLabel("连接线样式", 14f);
            headLabel.style.alignSelf = Align.FlexStart;
            headLabel.style.unityFontStyleAndWeight = FontStyle.Bold;
            headLabel.style.marginLeft = EditorElementUtility.defaultMarginHorizontal;
            column.Add(headLabel);


            ColorField connectionLineColorField = EditorElementUtility.elementFactory.CreateColorField("普通颜色:", connectionSetting.normalColor, newValue => connectionSetting.normalColor.value = newValue);
            column.Add(AddToContent(connectionLineColorField, () => connectionLineColorField.value = ConnectionLineSetting.defaultNormalColor));

            ColorField highlightColorField = EditorElementUtility.elementFactory.CreateColorField("高亮颜色:", connectionSetting.highlightColor, newValue => connectionSetting.highlightColor.value = newValue);
            column.Add(AddToContent(highlightColorField, () => highlightColorField.value = ConnectionLineSetting.defaultHighlightColor));

            return column;
        }


        private VisualElement CreateSelectionSetting()
        {
            Column column = EditorElementUtility.elementFactory.CreateColumn();
            column.style.marginTop = 2f;

            Label headLabel = EditorElementUtility.elementFactory.CreateLabel("选择样式", 14f);
            headLabel.style.alignSelf = Align.FlexStart;
            headLabel.style.unityFontStyleAndWeight = FontStyle.Bold;
            headLabel.style.marginLeft = EditorElementUtility.defaultMarginHorizontal;
            column.Add(headLabel);

            ColorField selectionBoxColorField = EditorElementUtility.elementFactory.CreateColorField("选择框颜色:", selectionSetting.selectBoxColor, newValue => selectionSetting.selectBoxColor.value = newValue);
            column.Add(AddToContent(selectionBoxColorField, () => selectionBoxColorField.value = SelectionSetting.defaultSelectBoxColor));

            return column;
        }

        private VisualElement AddToContent(VisualElement field, Action callback)
        {
            Button button = EditorElementUtility.elementFactory.CreateButton("重置", callback);
            return EditorElementUtility.MergeToRow(field, 1f, button, 0f, 0f);
        }
    }
}